package com.comac.print.component.app.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;


/**
 * 异常处理器
 * @author admin
 */
@RestControllerAdvice
public class ArgumentExceptionHandler {
    /**
     * 功能描述: @Validated 校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        List<FieldError> bindingResult = ex.getBindingResult().getFieldErrors();
        String errorMsg = "";
        StringBuilder builder = new StringBuilder();
        for(FieldError fieldError: bindingResult){
            errorMsg = fieldError.getField() + fieldError.getDefaultMessage()   + ";";
            builder.append(errorMsg);
        }
        return new ResponseEntity<>(builder, HttpStatus.NOT_FOUND);
        }
}
